using System.Text.Json;
using NetChinese.Interfaces;
using NetChinese.Models;

namespace NetChinese.Services
{
    /// <summary>
    /// JSON配置管理器
    /// </summary>
    public class JsonConfigManager : IConfigManager
    {
        private const string CONFIG_FILE = "config.json";

        public Config LoadConfig()
        {
            try
            {
                if (File.Exists(CONFIG_FILE))
                {
                    var json = File.ReadAllText(CONFIG_FILE);
                    return JsonSerializer.Deserialize<Config>(json) ?? new Config();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"加载配置文件失败: {ex.Message}");
            }
            return new Config();
        }

        public void SaveConfig(Config config)
        {
            try
            {
                var json = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
                File.WriteAllText(CONFIG_FILE, json);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"保存配置文件失败: {ex.Message}");
            }
        }
    }
} 